package com.intellagric.service.search.dao.agriecom_news;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.service.search.agriecom.pojo.AgriecomNewsResult;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class AgriecomNewsSearchDao {

    @Autowired
    @Qualifier("SolrServer3")
    private SolrServer solrServer;

    /**
     * 根据query查询索引库
     * @param query
     * @return
     * @throws SolrServerException
     */
    public LayuiDataGridResult query(SolrQuery query) throws SolrServerException {
        //执行查询
        QueryResponse response=solrServer.query(query);
        //获取查询结果
        SolrDocumentList resultList=response.getResults();
        //结果条数
        long resultCount=resultList.getNumFound();
        //高亮数据
        Map<String, Map<String, List<String>>> highlighting=response.getHighlighting();
        List<AgriecomNewsResult> list=new ArrayList<>();
        for(SolrDocument document:resultList){
            AgriecomNewsResult result= new AgriecomNewsResult();

            result.setId((String) document.get("id"));
            result.setBrief((String) document.get( "brief"));
            result.setCreateDate((Date) document.get("create_date"));
            result.setIsHot((String) document.get("is_hot"));
            result.setNewsDetail((String)document.get("news_detail"));
            result.setNewsTypeName((String)document.get("news_type_name"));
            result.setRemarks((String)document.get("remarks"));
            result.setSource((String)document.get("source"));

            //取title高亮
            List<String> titleHighlight = highlighting.get(document.get("id")).get("news_title");


            if(titleHighlight!=null&&titleHighlight.size()>0){
                System.out.println("size:"+titleHighlight.size());
                result.setTitle(titleHighlight.get(0));
            }else
                result.setTitle((String)document.get("news_title"));

            list.add(result);
        }
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.setData(list);
        lay.setCode(0);
        lay.setMsg("查询成功");
        lay.setCount(resultCount);
        return lay;
    }

    /**
     * 添加到索引库
     * @param result
     * @return
     */
    public ResponseMessage add(AgriecomNewsResult result){
        SolrInputDocument document=new SolrInputDocument();

        document.setField("id",result.getId());
        document.setField("news_title",result.getTitle());
        document.setField("brief",result.getBrief());
        document.setField("create_date",result.getCreateDate());
        document.setField("is_hot",result.getIsHot());
        document.setField("news_detail",result.getNewsDetail());
        document.setField("news_type_name",result.getNewsTypeName());
        document.setField("remarks",result.getRemarks());
        document.setField("source",result.getSource());
        try {
            solrServer.add(document);
            solrServer.commit();
        } catch (Exception e){
            e.printStackTrace();
            return ResponseMessage.fail("add falsely");
        }
        return ResponseMessage.success("add successfully");
    }


}
